/**
 * This file is part of the educational usermanagement system.
 * 
 * Copyright (c) 2008, Florian Arndt, Thomas Kemptner, Christian Kerl, Patrick Neuberger, Marcel Rehfeld, Emal Sadran, Stefan Sauer
 * 
 * Released under the MIT License http://www.opensource.org/licenses/mit-license.php
 */

package edu.ba.management;

import java.util.*;

/**
 * The User class represents a user in the management system.
 */
public class User implements Cloneable
{
    public static final int INVALID_ID = -1;

    private int id = INVALID_ID;
    private String firstName = "";
    private String lastName = "";
    private String eMail;
    private String birthday;
    private ArrayList<Integer> groups = new ArrayList<Integer>();

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#clone()
     */
    @Override
    public Object clone() throws CloneNotSupportedException
    {
        return super.clone();
    }

    /**
     * Gets the id.
     * 
     * @return the id
     */
    public int getId()
    {
        return id;
    }

    /**
     * Sets the id.
     * 
     * @param id the id to set
     */
    void setId(Integer id)
    {
        this.id = id;
    }

    /**
     * Gets the first name.
     * 
     * @return the firstName
     */
    public String getFirstName()
    {
        return firstName;
    }

    /**
     * Sets the first name.
     * 
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    /**
     * Gets the last name.
     * 
     * @return the lastName
     */
    public String getLastName()
    {
        return lastName;
    }

    /**
     * Sets the last name.
     * 
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    /**
     * Gets the email address.
     * 
     * @return the eMail
     */
    public String getEMail()
    {
        return eMail;
    }

    /**
     * Sets the email address.
     * 
     * @param mail the eMail to set
     */
    public void setEMail(String mail)
    {
        eMail = mail;
    }

    /**
     * Gets the birthday.
     * 
     * @return the birthday
     */
    public String getBirthday()
    {
        return birthday;
    }

    /**
     * Sets the birthday.
     * 
     * @param birthday the birthday to set
     */
    public void setBirthday(String birthday)
    {
        this.birthday = birthday;
    }

    /**
     * Gets the groups, the user is member of.
     * 
     * @return the groups
     */
    public List<Integer> getGroups()
    {
        return groups;
    }

    /**
     * Checks whether or not the user is member of the given group.
     * 
     * @param group the group to check against
     * @return true, if the user is member of the group, false otherwise
     */
    public boolean hasGroup(Group group)
    {
        return group != null && this.hasGroup(group.getId());
    }

    /**
     * Checks whether or not the user is member of the group with the given id.
     * 
     * @param group the group id to check against
     * @return true, if the user is member of the group, false otherwise
     */
    public boolean hasGroup(int group)
    {
        return this.groups.contains(group);
    }

    /**
     * Adds the user to the given group.
     * 
     * @param group the group to which the user should be added
     * @return false, when an error occurred, true otherwise
     */
    public boolean addGroup(Group group)
    {
        if (group != null && group.getId() != Group.INVALID_ID
                && !this.hasGroup(group.getId()) && this.getId() != INVALID_ID)
        {
            groups.add(group.getId());
            group.addUser(this);

            return true;
        }

        return false;
    }

    /**
     * Removes the user from the given group.
     * 
     * @param group the group from which the user should be removed
     * @return false, when an error occurred, true otherwise
     */
    public boolean removeGroup(Group group)
    {
        if (group != null && group.getId() != Group.INVALID_ID
                && this.hasGroup(group.getId()))
        {
            this.groups.remove((Object) group.getId());
            group.removeUser(this);

            return true;
        }

        return false;
    }

    /**
     * Gets the combination of first name and last name.
     * 
     * @return the full name
     */
    public String getFullName()
    {
        return this.getFirstName() + " " + this.getLastName();
    }
}
